/*
 * This file is part of PDBEdit.
 * 
 * PDBEdit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * PDBEdit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PDBEdit.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 31/08/2010
 * Copyright (c) Guerfyld 2010. All rights reserved.
 * http://......
 */
package pdbfile;

/**
 * The PDBNote class manage PilotDB Note field
 *
 * @author guerfyld
 * @version 1.0
 */
public class PDBNote extends PDBString {
	private int offValue;
	private String title;

	/**
	 * Constructor
	 *
	 * @param val	pilotDB field raw value
	 */
	public PDBNote(String ttl, String val) {
		super(val);
		offValue = 0;
		title = ttl;
	}

	/**
	 * Constructor
	 *
	 * @param stm	Stream to read
	 * @param pos	Start read position in stream
	 */
	public PDBNote(PDBInputStream stm, long pos) throws Exception {
		super(null);
		title = stm.readStringZ(pos);
		offValue = (int)stm.readValue(pos+title.length()+1, 2);
	}

	/**
	 * Set Offset of value
	 *
	 * @param off	Offset of note value
	 */
	public void setOffsetValue(int off) {
		offValue = off;
	}

	/**
	 * Set Title
	 *
	 * @param ttl	Title of note
	 */
	public void setTitle(String ttl) {
		title = ttl;
	}

	/**
	 * Get Offset of value
	 *
	 * @return	
	 */
	public int getOffsetValue() {
		return offValue;
	}

	/**
	 * Get Title
	 *
	 * @return	Title of note
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Returns a string representation of the object.
	 *
	 * @return	a string representation of the object.
	 */
	public String toString() {
		return title;
	}

	/**
	 * Returns length of raw data
	 */
	public long length() {
		return title.length()+ 3 + super.length();
	}

	/**
	 * Clone object
	 * @return	New object
	 */
	public PDBType clone() {
		PDBNote fld = new PDBNote(title, strVal);
		fld.setOffsetValue(offValue);
		return fld;
	}

	/**
	 * Write fields to stream
	 *
	 * &param stm	Stream to write
	 */
	public void write(PDBOutputStream stm) throws Exception {
		stm.writeString(title);
		stm.writeValue(offValue, 2);
		stm.writeString(getValue());
	}
}